"""
Phase 2: Baseline Regressions — Sovereign Spreads
====================================================
Tests whether demographic structure predicts:
  (a) sovereign credit ratings (21-point scale)
  (b) sovereign bond spreads (10y - world average)
  (c) sovereign spreads with standard determinants

Key question: do markets price demographic risk, or is it an
unpriced slow-moving risk factor?

Output: output/tables/baseline_ratings.md, baseline_spreads.md
"""

import sys
from pathlib import Path

import numpy as np
import pandas as pd

PROJECT_DIR = Path("/mnt/c/demographics_capital_flows/sovereign_spreads")
ROOT_DIR = PROJECT_DIR.parent
sys.path.insert(0, str(ROOT_DIR / "multilateral" / "src"))
from model import PanelGLS

PROCESSED_DIR = PROJECT_DIR / "data" / "processed"
TABLES_DIR = PROJECT_DIR / "output" / "tables"

OECD_38 = [
    "AUS", "AUT", "BEL", "CAN", "CHL", "COL", "CRI", "CZE", "DNK", "EST",
    "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", "IRL", "ISR", "ITA", "JPN",
    "KOR", "LVA", "LTU", "LUX", "MEX", "NLD", "NZL", "NOR", "POL", "PRT",
    "SVK", "SVN", "ESP", "SWE", "CHE", "TUR", "GBR", "USA",
]


def stars(p):
    if p < 0.01: return '***'
    if p < 0.05: return '**'
    if p < 0.10: return '*'
    return ''


def run_model(df, dep_var, regressors, label):
    """Run PanelGLS and return results dict."""
    regressors = [r for r in regressors if r in df.columns]
    if dep_var not in df.columns:
        print(f"  [{label}] {dep_var} missing — skipping")
        return None

    sub = df.dropna(subset=[dep_var] + regressors).copy()
    if len(sub) < 50:
        print(f"  [{label}] Insufficient obs ({len(sub)}) — skipping")
        return None

    gls = PanelGLS()
    gls.fit(sub[dep_var].values, sub[regressors].values,
            sub['iso3'].values, sub['year'].values)

    print(f"\n  [{label}]  N={gls.n_obs}, countries={gls.n_countries}, "
          f"R²={gls.r_squared:.4f}")

    results = {
        'label': label, 'dep_var': dep_var,
        'n_obs': gls.n_obs, 'n_countries': gls.n_countries,
        'r_squared': gls.r_squared, 'rho': gls.rho,
    }
    for i, name in enumerate(regressors):
        results[f'coef_{name}'] = gls.beta[i]
        results[f'se_{name}'] = gls.se[i]
        results[f'p_{name}'] = gls.pvalues[i]
        sig = stars(gls.pvalues[i])
        print(f"    {name:<25} {gls.beta[i]:>10.4f} ({gls.se[i]:.4f}) {sig}")

    return results


def build_table(results, key_vars, controls_label, filename, title):
    """Build markdown table from results list."""
    if not results:
        return

    md = [f"# {title}\n"]
    md.append("| Model | Dep Var | N | Countries | R² |")
    md.append("|---|---|---|---|---|")
    for r in results:
        md.append(f"| {r['label']} | {r['dep_var']} | {r['n_obs']:,} "
                  f"| {r['n_countries']} | {r['r_squared']:.3f} |")

    md.append("\n## Key Coefficients\n")
    md.append("| Model | Variable | Coef | SE | p-value | Sig |")
    md.append("|---|---|---|---|---|---|")
    for r in results:
        for var in key_vars:
            ckey = f'coef_{var}'
            if ckey in r:
                p = r[f'p_{var}']
                md.append(f"| {r['label']} | {var} | {r[ckey]:.4f} "
                          f"| {r[f'se_{var}']:.4f} | {p:.4f} | {stars(p)} |")

    md.append(f"\n*{controls_label}*")

    out = TABLES_DIR / filename
    out.write_text('\n'.join(md))
    print(f"\n  Saved: {out}")


def main():
    print("=" * 70)
    print("PHASE 2: Baseline Regressions — Sovereign Spreads")
    print("=" * 70)

    df = pd.read_csv(PROCESSED_DIR / "spread_panel.csv")
    print(f"Panel: {df['iso3'].nunique()} countries, {len(df):,} obs")

    demo_vars = ['Z_1', 'Z_2', 'Z_3']

    # ═══════════════════════════════════════════════════════════════════
    # PART A: Rating Models
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART A: RATING MODELS (dep var = rating_numeric, 21-point scale)")
    print("=" * 70)

    rating_results = []
    controls_rating = ['rgdp_growth', 'fiscal_bal_gdp', 'kaopen']

    # A1: Z → rating (no controls)
    r = run_model(df, 'rating_numeric', demo_vars, "A1: Z only")
    if r: rating_results.append(r)

    # A2: Z → rating (with controls)
    r = run_model(df, 'rating_numeric', demo_vars + controls_rating,
                  "A2: Z + controls")
    if r: rating_results.append(r)

    # A3: Z → rating (with debt/GDP)
    controls_debt = controls_rating + ['govt_debt_gdp']
    r = run_model(df, 'rating_numeric', demo_vars + controls_debt,
                  "A3: Z + controls + debt")
    if r: rating_results.append(r)

    # A4: old_dep, youth_dep → rating
    age_vars = ['old_dep', 'youth_dep']
    r = run_model(df, 'rating_numeric', age_vars + controls_rating,
                  "A4: age ratios + controls")
    if r: rating_results.append(r)

    # A5: OECD subsample
    oecd = df[df['iso3'].isin(OECD_38)].copy()
    r = run_model(oecd, 'rating_numeric', demo_vars + controls_debt,
                  "A5: OECD Z + controls + debt")
    if r: rating_results.append(r)

    # A6: non-OECD subsample
    non_oecd = df[~df['iso3'].isin(OECD_38)].copy()
    r = run_model(non_oecd, 'rating_numeric', demo_vars + controls_debt,
                  "A6: non-OECD Z + controls + debt")
    if r: rating_results.append(r)

    # A7: Income terciles
    for group in ['low', 'mid', 'high']:
        sub = df[df['income_group'] == group].copy()
        r = run_model(sub, 'rating_numeric', demo_vars + controls_rating,
                      f"A7{group[0]}: {group}-income")
        if r: rating_results.append(r)

    key_rating_vars = ['Z_1', 'Z_2', 'Z_3', 'old_dep', 'youth_dep', 'govt_debt_gdp']
    build_table(rating_results, key_rating_vars,
                f"Controls: {', '.join(controls_rating)}",
                "baseline_ratings.md",
                "Baseline: Demographics → Sovereign Ratings")

    # ═══════════════════════════════════════════════════════════════════
    # PART B: Spread Models
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART B: SPREAD MODELS (dep var = sovereign_spread, spread_vs_us)")
    print("=" * 70)

    spread_results = []
    controls_spread = ['rgdp_growth', 'fiscal_bal_gdp', 'kaopen', 'nfa_gdp_lag']

    # B1: Z → sovereign spread (world avg benchmark)
    r = run_model(df, 'sovereign_spread', demo_vars + controls_spread,
                  "B1: Z → spread (world)")
    if r: spread_results.append(r)

    # B2: Z → spread vs US
    r = run_model(df, 'spread_vs_us', demo_vars + controls_spread,
                  "B2: Z → spread (US)")
    if r: spread_results.append(r)

    # B3: Z → spread with debt/GDP
    controls_debt_spread = controls_spread + ['govt_debt_gdp']
    r = run_model(df, 'sovereign_spread', demo_vars + controls_debt_spread,
                  "B3: Z → spread + debt")
    if r: spread_results.append(r)

    # B4: Z → spread, controlling for rating
    controls_with_rating = controls_spread + ['rating_numeric']
    r = run_model(df, 'sovereign_spread', demo_vars + controls_with_rating,
                  "B4: Z → spread | rating")
    if r: spread_results.append(r)

    # B5: Z → spread vs DE (Europe)
    emu_countries = ['AUT', 'BEL', 'FIN', 'FRA', 'DEU', 'GRC', 'IRL', 'ITA',
                     'LUX', 'NLD', 'PRT', 'ESP', 'SVN', 'CYP', 'MLT', 'SVK',
                     'EST', 'LVA', 'LTU']
    emu = df[df['iso3'].isin(emu_countries)].copy()
    r = run_model(emu, 'spread_vs_de', demo_vars + controls_spread,
                  "B5: EMU Z → spread vs DE")
    if r: spread_results.append(r)

    # B6: age ratios → spread
    r = run_model(df, 'sovereign_spread', age_vars + controls_spread,
                  "B6: age ratios → spread")
    if r: spread_results.append(r)

    # B7: short spread
    r = run_model(df, 'short_spread', demo_vars + controls_spread,
                  "B7: Z → short spread")
    if r: spread_results.append(r)

    # B8: log spread vs US (EM positive spreads only)
    r = run_model(df, 'log_spread_vs_us', demo_vars + controls_spread,
                  "B8: Z → log spread vs US")
    if r: spread_results.append(r)

    key_spread_vars = ['Z_1', 'Z_2', 'Z_3', 'old_dep', 'youth_dep',
                       'govt_debt_gdp', 'rating_numeric']
    build_table(spread_results, key_spread_vars,
                f"Controls: {', '.join(controls_spread)}",
                "baseline_spreads.md",
                "Baseline: Demographics → Sovereign Spreads")

    # ═══════════════════════════════════════════════════════════════════
    # PART C: 5-year lag & first-differenced demographics
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART C: LAGGED & FIRST-DIFFERENCED DEMOGRAPHICS")
    print("=" * 70)

    lag_results = []
    lag_vars = ['Z_1_lag5', 'Z_2_lag5', 'Z_3_lag5']
    diff_vars = ['d_Z_1', 'd_Z_2', 'd_Z_3']

    # C1: 5yr lag → rating
    r = run_model(df, 'rating_numeric', lag_vars + controls_rating,
                  "C1: Z_lag5 → rating")
    if r: lag_results.append(r)

    # C2: 5yr lag → spread
    r = run_model(df, 'sovereign_spread', lag_vars + controls_spread,
                  "C2: Z_lag5 → spread")
    if r: lag_results.append(r)

    # C3: first-diff → rating
    r = run_model(df, 'rating_numeric', diff_vars + controls_rating,
                  "C3: ΔZ → rating")
    if r: lag_results.append(r)

    # C4: first-diff → spread
    r = run_model(df, 'sovereign_spread', diff_vars + controls_spread,
                  "C4: ΔZ → spread")
    if r: lag_results.append(r)

    # C5: predetermined OADR → rating
    r = run_model(df, 'rating_numeric', ['oadr_plus20'] + controls_rating,
                  "C5: OADR+20 → rating")
    if r: lag_results.append(r)

    key_lag_vars = ['Z_1_lag5', 'Z_2_lag5', 'Z_3_lag5',
                    'd_Z_1', 'd_Z_2', 'd_Z_3', 'oadr_plus20']
    build_table(lag_results, key_lag_vars,
                "Lagged and differenced demographics",
                "lagged_demographics.md",
                "Lagged & First-Differenced Demographics")

    print("\n" + "=" * 70)
    print("Phase 2 complete.")
    print("=" * 70)


if __name__ == "__main__":
    main()
